/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package preka.prefuse.action;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;

import prefuse.Visualization;
import prefuse.action.GroupAction;
import prefuse.data.expression.Predicate;
import prefuse.visual.VisualItem;
import prefuse.visual.expression.VisiblePredicate;
import preka.IVC;
import preka.prefuse.visual.VisualInstance;
import preka.prefuse.visual.VisualInstances;
import weka.clusterers.FarthestFirst;

/**
 * TODO escrever descrição
 * {@link #setFilterPredicate(Predicate)}
 *
 * @author <a href="http://vhbarros.org">Vitor Hugo Barros</a>
 */
public class RandomMove extends Heuristic {

    /** A reference to filtering predicate for this Action */
    //protected VisualInstances m_visualInstances;

    /**
     * Create a new IVCFarthestFirst that processes all groups.
     * @see Visualization#ALL_ITEMS
     */
    public RandomMove() {
        this(null, Visualization.ALL_ITEMS);
    }

    /**
     * Create a new IVCFarthestFirst that processes all groups.
     * @param vis the {@link prefuse.Visualization} to process
     * @see Visualization#ALL_ITEMS
     */
    /*public IVCFarthestFirst(Visualization vis) {
        this(vis, Visualization.ALL_ITEMS);
    }*/

    /**
     * Create a new IVCFarthestFirst that processes the specified group.
     * @param group the name of the group to process
     */
    public RandomMove(IVC ivc) {
        this(ivc, Visualization.ALL_ITEMS);
    }

    /**
     * Create a new IVCFarthestFirst that processes the specified group.
     * @param group the name of the group to process
     * @param vinsts visual instances associated with the IVC process
     */
    /*public IVCFarthestFirst(String group, VisualInstances vinsts) {
        this(null, group, vinsts);
    }*/

    /**
     * Create a new IVCFarthestFirst that processes the specified group.
     * @param vis the {@link prefuse.Visualization} to process
     * @param group the name of the group to process
     */
    public RandomMove(IVC ivc, String group) {
        //this(vis, group, null);
        super(ivc, group);
    }

    /**
     * Create a new IVCFarthestFirst that processes the specified group.
     * @param vis the {@link prefuse.Visualization} to process
     * @param group the name of the group to process
     * @param vinsts visual instances associated with the IVC process
     */
    /*public IVCFarthestFirst(Visualization vis, String group, VisualInstances vinsts) {
        super(vis, group);
        m_visualInstances = vinsts;
    }*/

    // ------------------------------------------------------------------------

    /**
     * @see prefuse.action.Action#run(double)
     */
    //public void run(double frac) {
    //    System.out.println("Hey! frac:"+frac);
    //}
    public void moveNode() {
        VisualInstance randomNode;
        double [] pos;

        randomNode = getRandomMisplacedNode();
        if (randomNode != null) { // TODO: Workaround!! Perceber melhor razão de devolver nulo!!
            pos = getRandomClusterPosition(getHeuristicCluster(randomNode.getClassName()));

            System.out.println("Instância: "+randomNode.getNumInstance()+
                               " Cluster: "+getHeuristicCluster(randomNode.getClassName())+
                               " X: "+pos[0]+" Y:"+pos[1]);

            moveInstance(randomNode, pos[0], pos[1]);
        }
    }

    private VisualInstance getRandomMisplacedNode() {
        
        VisualInstance currNode = null;
        Random rnd;
        int index;
        String currClassName;

        ArrayList notMovedVisualInstances =  m_IVC.getVisualInstances().getNotMovedVisualInstances(m_IVC.getVisualization(),
                                                                                                   IVC.NODES_GROUP);

        ArrayList notMovedNodes = new ArrayList();

        Iterator instances = notMovedVisualInstances.iterator();
        while (instances.hasNext()){
            currNode = (VisualInstance) instances.next();
            currClassName = getHeuristicClassName(currNode.getVisualCluster());
            if (!currNode.getClassName().equals(currClassName))
                notMovedNodes.add(currNode);
        }

        currNode = null;
        rnd = new Random();
        index = rnd.nextInt(notMovedNodes.size());
        
        currNode = (VisualInstance) notMovedNodes.get(index);
        currClassName = getHeuristicClassName(currNode.getVisualCluster());
        System.out.println("Instance:"+currNode.getNumInstance()+" To Class:"+currNode.getClassName()+" Current Class:"+currClassName);

        return currNode;
    }




}